package com.telecom.cms.util;

import java.util.ResourceBundle;
import java.util.HashMap;

import java.security.*;

import sun.misc.BASE64Encoder;

/**
 * @Author Ruyi.Luo
 * 
 */
public class Constant {

	public static final String myContainer = getStPara("SERVERTYPE");

	// public static final String myContainer="JBOSS";

	public static final String SEG_DATABASE = getStPara("DATABASE"); 

	public static final String SEG_BOOKMARK_DEF = SEG_DATABASE
			+ "SEG_BOOKMARK_DEF";

	public static final String SEG_USER_FOLDER_DEF = SEG_DATABASE
			+ "SEG_USER_FOLDER_DEF"; 

	public static final String SEG_APP_SUBJECT_DEF = SEG_DATABASE
			+ "SEG_APP_SUBJECT_DEF";

	public static final String SEG_SEG_RULE_DEF = SEG_DATABASE
			+ "SEG_SEG_RULE_DEF";

	public static final String SEG_SEG_RULE_SHOWATOM = SEG_DATABASE
			+ "SEG_SEG_RULE_SHOWATOM";

	public static final String SEG_ATOM_GROUP_REL = SEG_DATABASE
			+ "SEG_ATOM_GROUP_REL";

	public static final String SEG_ATOM_SHOW_TYPE_DEF = SEG_DATABASE
			+ "SEG_ATOM_SHOW_TYPE_DEF";

	 public static final String SEG_BIZ_ATOM_DEF=SEG_DATABASE+"SEG_BIZ_ATOM_DEF";
//	public static final String SEG_BIZ_ATOM_DEF = "td_cms.SEG_BIZ_ATOM_DEF";

	public static final String SEG_BIZ_GROUP_DEF = SEG_DATABASE
			+ "SEG_BIZ_GROUP_DEF";

	public static final String CUST_CLU = SEG_DATABASE + "CUST_CLU";

	public static final String CUST_CLU_RES = SEG_DATABASE + "CUST_CLU_RES";

	public static final String DMN_BUREAU = SEG_DATABASE + "DMN_BUREAU";

	public static final String LOC_LATN = SEG_DATABASE + "DMN_LOC_LATN";

	public static final String camp_cust_info = SEG_DATABASE + "camp_cust_info";

	public static final String CAMP_CUST_APPD_RSLT = SEG_DATABASE
			+ "CAMP_CUST_APPD_RSLT";

	// 日志
	public static final String SEG_USER_SQL_LOG = SEG_DATABASE
			+ "SEG_USER_SQL_LOG";

	public static final String SEG_USER_EXP_LOG = SEG_DATABASE
			+ "SEG_USER_EXP_LOG";

	// 旧模板
	public static final String SEG_SUBJECT_TEMPLATE_DEF = SEG_DATABASE
			+ "SEG_SUBJECT_TEMPLATE_DEF";

	public static final String SEG_TEMPLATE_ATOM_DEF = SEG_DATABASE
			+ "SEG_TEMPLATE_ATOM_DEF";

	// 新模板
	public static final String IDA_TEMPLATE = SEG_DATABASE + "IDA_TEMPLATE";

	public static final String IDA_TEMPLATE_ATOM_PARAM = SEG_DATABASE
			+ "IDA_TEMPLATE_ATOM_PARAM";

	public static final String IDA_TEMPLATE_ATOM_RESULT = SEG_DATABASE
			+ "IDA_TEMPLATE_ATOM_RESULT";

	public static final String IDA_TEMPLATE_ATOM_SQL = SEG_DATABASE
			+ "IDA_TEMPLATE_ATOM_SQL ";

	public static final String IDA_TEMPLATE_INSTANCE_PARAM = SEG_DATABASE
			+ "IDA_TEMPLATE_INSTANCE_PARAM";

	public static final String IDA_TEMPLATE_REL = SEG_DATABASE
			+ "IDA_TEMPLATE_REL";

	// 库
	public static final String MART_DATABASE = "PD_MART.";

	public static final String T_MART_DATABASE = "TD_MART.";

	public static final String FCT_DEF_CUST_CLU_F = MART_DATABASE
			+ "FCT_DEF_CUST_CLU_F";

	public static final String BAS_PRD_INST_HIST_F = MART_DATABASE
			+ "BAS_PRD_INST_HIST_F";

	public static final String SEG_APPLY_CUBE_DEF = SEG_DATABASE
			+ "SEG_APPLY_CUBE_DEF";

	private static HashMap paraMap = new HashMap();

	private String paraVale;

	public static String getStPara(String paraName) {
		// get province info
		ResourceBundle bundle = ResourceBundle.getBundle("Para");
		String provname = bundle.getString("PROVINCE").trim();
		
		if (paraMap.get("ALLPROVINCE").equals(provname)&&paraMap.get(paraName.trim()) != null) {
			return paraMap.get(paraName.trim()).toString();
		}
		paraMap.put("ALLPROVINCE", provname);

		// get parameter from province file
		bundle = ResourceBundle.getBundle("Para_" + provname);
		paraMap.put(paraName.trim(), bundle.getString(paraName.trim()).trim());
		return bundle.getString(paraName.trim()).trim();
	}

	public String getPara(String paraName) {
		// System.out.println(paraName);
		if (null != paraMap.get(paraName.trim())) {
			paraVale = paraMap.get(paraName.trim()).toString();
			return paraVale;
		}

		// get province info
		ResourceBundle bundle = ResourceBundle.getBundle("Para");
		String provname = bundle.getString("PROVINCE").trim();

		// get parameter from province file
		bundle = ResourceBundle.getBundle("Para_" + provname);
		paraVale = bundle.getString(paraName.trim()).trim();
		paraMap.put(paraName.trim(), paraVale);
		return paraVale;
	}

	public String setPara(int parapos, String paraValue) {
		paraVale = paraVale.replace("{" + parapos + "}", paraValue);
		return paraVale;
	}

	// this function is to change view partition by latn
	// such as pv_mart_Z to pv_mart_d etc.
	public static String getLatnPara(String paraName, String latn_cd) {
		String returnsql = getStPara(paraName);

		String baslatncd = getStPara("BASLATNCD");
		returnsql = returnsql.replaceAll("_" + baslatncd + " ", "_" + latn_cd
				+ " ");
		returnsql = returnsql.replaceAll("_" + baslatncd + "\\.", "_" + latn_cd
				+ ".");
		returnsql = returnsql.replaceAll("_" + baslatncd + ",", "_" + latn_cd
				+ ",");
		return returnsql;
	}

	public static String changeLatnId(String latn_cd, String sql) {

		String returnsql = sql;
		String baslatncd = getStPara("BASLATNCD");
		returnsql = returnsql.replaceAll("_" + baslatncd + " ", "_" + latn_cd
				+ " ");
		returnsql = returnsql.replaceAll("_" + baslatncd + "\\.", "_" + latn_cd
				+ ".");
		returnsql = returnsql.replaceAll("_" + baslatncd + ",", "_" + latn_cd
				+ ",");
		return returnsql;
	}

	public static String EncoderByMd5(String str) {
		MessageDigest md5;
		String newstr = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base64en = new BASE64Encoder();
			newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newstr;
	}

	public static String Encypt1() {
		String srcstr = com.telecom.cms.util.Constant.getStPara("CONNSTR")
				+ com.telecom.cms.util.Constant.getStPara("DATABASE");
		return EncoderByMd5(srcstr);
	}

	public static String Encypt2() {
		String srcstr = com.telecom.cms.util.Constant.getStPara("CONNSTR")
				+ com.telecom.cms.util.Constant.getStPara("DATABASE");
		return EncoderByMd5(srcstr + srcstr);
	}

	public static String EncyptN(int n) {
		String srcstr = "";
		for (int i = 0; i < n; i++)
			srcstr += com.telecom.cms.util.Constant.getStPara("CONNSTR");
		return EncoderByMd5(srcstr);
	}

	public static void TestAuth() {
		for (int i = 1; i < 10; i++)
			System.out.println(EncyptN(i));
	}

	public static long EasyEnc(long chr) {
		chr *= 1000;
		chr += 60000;
		return chr ^ (long) 12345;
	}

	public static long EasyDec(long chr) {
		chr ^= 12345;
		chr -= 60000;
		chr /= 1000;
		return chr;
	}

	public static String EasyCode(String chr) {
		String rlt = "";
		for (int i = 0; i < chr.length(); i++)
			rlt = rlt + (char) (chr.charAt(i) ^ 'a');
		return rlt;
	}

	public static int CheckAuth() {
		for (int i = 1; i < 10; i++)
			if (com.telecom.cms.util.Constant.getStPara("MD5").equals(
					EncyptN(i)))
				return i;
		return -1;
	}

	public static void println(Object str) {
		if (com.telecom.cms.util.Constant.getStPara("OUTDEBUG").equals("TRUE"))
			System.out.println(str);
	}

}
